package com.lmn.ribbon_simple;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class LoadBalanceController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancer;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String getRequest() {
        ServiceInstance instance = loadBalancer.choose("helloword-service");
        String host = instance.getHost();
        int port = instance.getPort();
        System.out.println("负载均衡请求：" + host + ":" + port);
        return "负载均衡请求：" + host + ":" + port;
    }
}
